﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.ComponentModel;
using libConn;

namespace ApartmentSale
{
    public class ApartmentView : INotifyPropertyChanged , ICloneable
    {
        private int _apartment_id;
        public int apartment_id
        { 
            get
            { 
                return _apartment_id; 
            }
            set
            {
                _apartment_id = value;
                NotifyPropertyChanged("apartment_id");
            }

        }

        private int _apartment_status;
        public int apartment_status
        {
            get
            {
                return _apartment_status;
            }
            set
            {
                _apartment_status = value;
                NotifyPropertyChanged("apartment_status");
            }
        }

        private string _apartment_code;
        public string apartment_code
        {
            get
            {
                return _apartment_code;
            }
            set
            {
                _apartment_code = value;
                NotifyPropertyChanged("apartment_code");
            }
        }

        private string _apartment_building;
        public string apartment_building
        {
            get
            {
                return _apartment_building;
            }
            set
            {
                _apartment_building = value;
                NotifyPropertyChanged("apartment_building");
            }
        }

        private int _apartment_floor;
        public int apartment_floor
        {
            get
            {
                return _apartment_floor;
            }
            set
            {
                _apartment_floor = value;
                NotifyPropertyChanged("apartment_floor");
            }
        }

        private string _apartment_unit;
        public string apartment_unit
        {
            get
            {
                return _apartment_unit;
            }
            set
            {
                _apartment_unit = value;
                NotifyPropertyChanged("apartment_unit");
            }
        }

        private int _apartment_sn;
        public int apartment_sn
        {
            get
            {
                return _apartment_sn;
            }
            set
            {
                _apartment_sn = value;
                NotifyPropertyChanged("apartment_sn");
            }
        }

        private double _apartment_built_area;
        public double apartment_built_area
        {
            get
            {
                return _apartment_built_area;
            }
            set
            {
                _apartment_built_area = value;
                NotifyPropertyChanged("apartment_built_area");
            }
        }

        private double _apartment_public_area;
        public double apartment_public_area
        {
            get
            {
                return _apartment_public_area;
            }
            set
            {
                _apartment_public_area = value;
                NotifyPropertyChanged("apartment_public_area");
            }
        }

        private double _apartment_area;
        public double apartment_area 
        {
            get
            {
                return _apartment_area;
            }
            set
            {
                _apartment_area = value;
                NotifyPropertyChanged("apartment_area");
            }
        }

        private double _apartment_price;
        public double apartment_price 
        {
            get
            {
                return _apartment_price;
            }
            set
            {
                _apartment_price = value;
                NotifyPropertyChanged("apartment_price");
            }
        }

        private double _apartment_total;
        public double apartment_total 
        {
            get
            {
                return _apartment_total;
            }
            set
            {
                _apartment_total = value;
                NotifyPropertyChanged("apartment_total");
            }
        }

        private bool _apartment_decorating;
        public bool apartment_decorating 
        {
            get
            {
                return _apartment_decorating;
            }
            set
            {
                _apartment_decorating = value;
                NotifyPropertyChanged("apartment_decorating");
            }
        }

        private double _apartment_decorating_price;
        public double apartment_decorating_price 
        {
            get
            {
                return _apartment_decorating_price;
            }
            set
            {
                _apartment_decorating_price = value;
                NotifyPropertyChanged("apartment_decorating_price");
            }
        }

        private double _apartment_decorating_total;
        public double apartment_decorating_total 
        {
            get
            {
                return _apartment_decorating_total;
            }
            set
            {
                _apartment_decorating_total = value;
                NotifyPropertyChanged("apartment_decorating_total");
            }
        }

        private double _apartment_rebate_amount;
        public double apartment_rebate_amount
        {
            get
            {
                return _apartment_rebate_amount;
            }
            set
            {
                _apartment_rebate_amount = value;
                NotifyPropertyChanged("apartment_rebate_amount");
            }
        }

        private double _apartment_accounts_due;
        public double apartment_accounts_due 
        {
            get
            {
                return _apartment_accounts_due;
            }
            set
            {
                _apartment_accounts_due = value;
                NotifyPropertyChanged("apartment_accounts_due");
            }
        }

        private double _apartment_invoice_amount;
        public double apartment_invoice_amount 
        {
            get
            {
                return _apartment_invoice_amount;
            }
            set
            {
                _apartment_invoice_amount = value;
                NotifyPropertyChanged("apartment_invoice_amount");
            }
        }

        private int _apartment_payment_term;
        public int apartment_payment_term 
        {
            get
            {
                return _apartment_payment_term;
            }
            set
            {
                _apartment_payment_term = value;
                NotifyPropertyChanged("apartment_payment_term");
            }
        }

        private int _apartment_payment_status;
        public int apartment_payment_status 
        {
            get
            {
                return _apartment_payment_status;
            }
            set
            {
                _apartment_payment_status = value;
                NotifyPropertyChanged("apartment_payment_status");
            }
        }

        private double _apartment_payment_amount;
        public double apartment_payment_amount 
        {
            get
            {
                return _apartment_payment_amount;
            }
            set
            {
                _apartment_payment_amount = value;
                NotifyPropertyChanged("apartment_payment_amount");
            }
        }

        private double _apartment_payment_paid_amount;
        public double apartment_payment_paid_amount 
        {
            get
            {
                return _apartment_payment_paid_amount;
            }
            set
            {
                _apartment_payment_paid_amount = value;
                NotifyPropertyChanged("apartment_payment_paid_amount");
            }
        }

        private double _apartment_payment_unpaid_amount;
        public double apartment_payment_unpaid_amount 
        {
            get
            {
                return _apartment_payment_unpaid_amount;
            }
            set
            {
                _apartment_payment_unpaid_amount = value;
                NotifyPropertyChanged("apartment_payment_unpaid_amount");
            }
        }

        private double _apartment_payment_bank_amount;
        public double apartment_payment_bank_amount 
        {
            get
            {
                return _apartment_payment_bank_amount;
            }
            set
            {
                _apartment_payment_bank_amount = value;
                NotifyPropertyChanged("apartment_payment_bank_amount");
            }
        }

        private double _apartment_payment_bank_income;
        public double apartment_payment_bank_income 
        {
            get
            {
                return _apartment_payment_bank_income;
            }
            set
            {
                _apartment_payment_bank_income = value;
                NotifyPropertyChanged("apartment_payment_bank_income");
            }
        }

        private string _apartment_host_name;
        public string apartment_host_name 
        {
            get
            {
                return _apartment_host_name;
            }
            set
            {
                _apartment_host_name = value;
                NotifyPropertyChanged("apartment_host_name");
            }
        }

        private string _apartment_host_contact;
        public string apartment_host_contact 
        {
            get
            {
                return _apartment_host_contact;
            }
            set
            {
                _apartment_host_contact = value;
                NotifyPropertyChanged("apartment_host_contact");
            }
        }

        private string _apartment_discription;
        public string apartment_discription
        {
            get
            {
                return _apartment_discription;
            }
            set
            {
                _apartment_discription = value;
                NotifyPropertyChanged("apartment_discription");
            }
        }

        public string status_string
        {
            get
            {
                string str = "";
                switch (apartment_status)
                {
                    case 0:
                        str = "Forsale";
                        break;
                    case 1:
                        str = "Deposited";
                        break;
                    case 2:
                        str = "Saled";
                        break;
                }
                return str;
            }
            set
            {
                switch (value)
                {
                    case "Forsale":
                        apartment_status = 0;
                        break;
                    case "Deposited":
                        apartment_status = 1;
                        break;
                    case "Saled":
                        apartment_status = 2;
                        break;
                    default:
                        apartment_status = 0;
                        break;
                }
            }
        }

        public string price_string
        {
            get
            {
                return String.Format("{0:N2}", apartment_price);
            }
        }

        public string area_string
        {
            get
            {
                return String.Format("{0:N2}", apartment_area);
            }
        }

        public string total_string
        {
            get
            {
                return String.Format("{0:N2}", apartment_total);
            }
        }

        public string paid_string
        {
            get
            {
                return String.Format("{0:N2}", apartment_payment_paid_amount);
            }
        }

        public string unpaid_string
        {
            get
            {
                return String.Format("{0:N2}", apartment_payment_unpaid_amount);
            }
        }

        public string payment_term
        {
            get
            {
                string str = "";
                switch (apartment_payment_term)
                {
                    case 0:
                        str = "Lump Sum";
                        break;
                    case 1:
                        str = "Instalment";
                        break;
                    case 2:
                        str = "Mortgage Loan";
                        break;
                }
                return str;
            }
            set
            {
                switch (value)
                {
                    case "Lump Sum":
                        apartment_payment_term = 0;
                        break;
                    case "Instalment":
                        apartment_payment_term = 1;
                        break;
                    case "Mortgage Loan":
                        apartment_payment_term = 2;
                        break;
                    default:
                        apartment_payment_term = 0;
                        break;
                }
            }
        }

        public string payment_status
        {
            get
            {
                string str = "";
                switch (apartment_payment_status)
                {
                    case 0:
                        str = "Non-payment";
                        break;
                    case 1:
                        str = "Outstanding";
                        break;
                    case 2:
                        str = "Liquidated";
                        break;
                }
                return str;
            }
            set
            {
                switch (value)
                {
                    case "Non-payment":
                        apartment_payment_status = 0;
                        break;
                    case "Outstanding":
                        apartment_payment_status = 1;
                        break;
                    case "Liquidated":
                        apartment_payment_status = 2;
                        break;
                    default:
                        apartment_payment_status = 0;
                        break;
                }
            }
        }

        public string resume
        {
            get
            {
                return "Price:" + apartment_price.ToString("0")
                    + " Area:" + apartment_area.ToString("0")
                    + " Amount:" + apartment_total.ToString("0");
            }
        }

        public bool has_changed { get; set; }

        public string old_apartment_code { get; set; }

        public override string ToString()
        {
            return apartment_code.ToString();
        }

        public ApartmentView(DataRow r)
        {
            apartment_id = Convert.ToInt32(r["apartment_id"]);
            apartment_status = Convert.ToInt32(r["apartment_status"]);
            apartment_code = r["apartment_code"].ToString().Replace("''", "'");
            apartment_building = r["apartment_building"].ToString().Replace("''", "'");
            apartment_floor = Convert.ToInt32(r["apartment_floor"]);
            apartment_unit = r["apartment_unit"].ToString().Replace("''", "'");
            apartment_sn = Convert.ToInt32(r["apartment_sn"]);
            apartment_built_area = Convert.ToDouble(r["apartment_built_area"]);
            apartment_public_area = Convert.ToDouble(r["apartment_public_area"]);
            apartment_area = Convert.ToDouble(r["apartment_area"]);
            apartment_price = Convert.ToDouble(r["apartment_price"]);
            apartment_total = Convert.ToDouble(r["apartment_total"]);
            apartment_decorating = Convert.ToBoolean(r["apartment_decorating"]);
            apartment_decorating_price = Convert.ToDouble(r["apartment_decorating_price"]);
            apartment_decorating_total = Convert.ToDouble(r["apartment_decorating_total"]);
            apartment_rebate_amount = Convert.ToDouble(r["apartment_rebate_amount"]);
            apartment_accounts_due = Convert.ToDouble(r["apartment_accounts_due"]);
            apartment_invoice_amount = Convert.ToDouble(r["apartment_invoice_amount"]);
            apartment_payment_term = Convert.ToInt32(r["apartment_payment_term"]);
            apartment_payment_status = Convert.ToInt32(r["apartment_payment_status"]);
            apartment_payment_amount = Convert.ToDouble(r["apartment_payment_amount"]);
            apartment_payment_paid_amount = Convert.ToDouble(r["apartment_payment_paid_amount"]);
            apartment_payment_unpaid_amount = Convert.ToDouble(r["apartment_payment_unpaid_amount"]);
            apartment_payment_bank_amount = Convert.ToDouble(r["apartment_payment_bank_amount"]);
            apartment_payment_bank_income = Convert.ToDouble(r["apartment_payment_bank_income"]);
            apartment_host_name = r["apartment_host_name"].ToString().Replace("''", "'");
            apartment_host_contact = r["apartment_host_contact"].ToString().Replace("''", "'");
            apartment_discription = r["apartment_discription"].ToString().Replace("''", "'");

            Datarow = r;
            has_changed = false;
            old_apartment_code = apartment_code;
        }

        public ApartmentView()
        {
            apartment_id = 0;
            apartment_status = 0;
            apartment_code = "";
            apartment_building = "";
            apartment_floor = 0;
            apartment_unit = "";
            apartment_sn = 0;
            apartment_built_area = 0;
            apartment_public_area = 0;
            apartment_area = 0;
            apartment_price = 0;
            apartment_total = 0;
            apartment_decorating = false;
            apartment_decorating_price = 0;
            apartment_decorating_total = 0;
            apartment_rebate_amount = 0;
            apartment_accounts_due = 0;
            apartment_invoice_amount = 0;
            apartment_payment_term = 0;
            apartment_payment_status = 0;
            apartment_payment_amount = 0;
            apartment_payment_paid_amount = 0;
            apartment_payment_unpaid_amount = 0;
            apartment_payment_bank_amount = 0;
            apartment_payment_bank_income = 0;
            apartment_host_name = "";
            apartment_host_contact = "";
            apartment_discription = "";

            initDataRow();
            has_changed = false;
            old_apartment_code = "";
        }

        private DataRow _datarow;
        public DataRow Datarow
        {
            set
            {
                _datarow = value;
            }
            get
            {
                refreshDataRow();
                return _datarow;
            }
        }

        public string queryString
        {
            get
            {
                return "SELECT * FROM apartments WHERE apartment_id = " + apartment_id;
            }
        }
        public string deleteString
        {
            get
            {
                return "DELETE FROM apartments WHERE apartment_id = " + apartment_id;
            }
        }

        public readonly string tableName = "apartments";
        public readonly int ColumnCount = 28;

        private void initDataRow()
        {
            string errMessage = "";
            DataTable table = AccdbConn.query(queryString, ref errMessage);
            Datarow = table.NewRow();
        }

        private void refreshDataRow()
        {
            DataRow r = _datarow;
            r["apartment_id"] = apartment_id;
            r["apartment_status"] = apartment_status;
            r["apartment_code"] = apartment_code.Replace("'", "''");
            r["apartment_building"] = apartment_building.Replace("'", "''");
            r["apartment_floor"] = apartment_floor;
            r["apartment_unit"] = apartment_unit.Replace("'", "''");
            r["apartment_sn"] = apartment_sn;
            r["apartment_built_area"] = apartment_built_area;
            r["apartment_public_area"] = apartment_public_area;
            r["apartment_area"] = apartment_area;
            r["apartment_price"] = apartment_price;
            r["apartment_total"] = apartment_total;
            r["apartment_decorating"] = apartment_decorating;
            r["apartment_decorating_price"] = apartment_decorating_price;
            r["apartment_decorating_total"] = apartment_decorating_total;
            r["apartment_rebate_amount"] = apartment_rebate_amount;
            r["apartment_accounts_due"] = apartment_accounts_due;
            r["apartment_invoice_amount"] = apartment_invoice_amount;
            r["apartment_payment_term"] = apartment_payment_term;
            r["apartment_payment_status"] = apartment_payment_status;
            r["apartment_payment_amount"] = apartment_payment_amount;
            r["apartment_payment_paid_amount"] = apartment_payment_paid_amount;
            r["apartment_payment_unpaid_amount"] = apartment_payment_unpaid_amount;
            r["apartment_payment_bank_amount"] = apartment_payment_bank_amount;
            r["apartment_payment_bank_income"] = apartment_payment_bank_income;
            r["apartment_host_name"] = apartment_host_name.Replace("'", "''");
            r["apartment_host_contact"] = apartment_host_contact.Replace("'", "''");
            r["apartment_discription"] = apartment_discription.Replace("'", "''");
        }

        public string Insert()
        {
            string errMessage = "";

            if (!AccdbConn.insert(queryString, tableName, ColumnCount, Datarow, ref errMessage))
            {
                errMessage = "Add housing data failed.\n" + errMessage;
                has_changed = true;
            }
            else
            {
                has_changed = false;
            }
            return errMessage;
        }

        public string Update()
        {
            string errMessage = "";

            if (!AccdbConn.update(queryString, tableName, ColumnCount, Datarow, ref errMessage))
            {
                errMessage = "Modify the housing data failed.\n" + errMessage;
                has_changed = true;
            }
            else
            {
                has_changed = false;
            }

            return errMessage;
        }

        public string Delete()
        {
            string errMessage = "";
            AccdbConn.exec(deleteString, ref errMessage);

            if (errMessage != "")
                errMessage = "Remove the housing data failed.\n" + errMessage;

            return errMessage;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                has_changed = true;
            }
        }

        public object Clone()
        {
            return new ApartmentView(Datarow);
        }
    }
}
